<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/21/021
 * Time: 10:56
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\User\User;

class OptionOrderStartTime
{
    /** @var Order $order */
    private $order;
    /** @var User $user */
    private $user;
    private $orderStartTime;
    public function __construct(Order $order,User $user)
    {
        $this->order = $order;
        $this->user  = $user;
    }


    public function setOrderStart($orderStartTime){
        $this->orderStartTime = $orderStartTime;
    }

    /**
     * @throws BlException
     */
    public function choiceTime(){
        if(!$this->order->is_wait_status()){
            throw new BlException('工单状态不允许');
        }
//        if($this->orderStartTime<time()){
//            throw new BlException('开工时间必须大于现在时间');
//        }
//        $startingTime  = $this->order->get_starting_time();
        $orderDb = (new OrderInfo());
        $data = [
            $orderDb->_id=>$this->order->get_order_id(),
            $orderDb->_starting_time=>$this->orderStartTime
        ];
        $result = $orderDb->update($data);
        if(!$result){
            throw new BlException('修改开工时间失败');
        }
        $this->order->delAllCache();
        $model =  (new OrderPush($this->order));
        $model->startPush();
        if($this->order->get_site_supervisor_uid() >0){
            $model->startPushInternal();
        }
        return true;
    }
}